package org.pps.gl.client.vue;

import org.pps.gl.client.events.ConnectedEvent;
import org.pps.gl.client.events.ConnectedEventHandler;
import org.pps.gl.client.events.SignOutEvent;
import org.pps.gl.client.events.SignOutEventHandler;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * La bare d'onglet qui sert de menu horizontal
 * 
 */
public class Corps extends Composite implements ConnectedEventHandler,
		SignOutEventHandler, ValueChangeHandler<String> {

	private VerticalPanel vp;
	private HandlerRegistration hr;
	TabLayoutPanel decoratedTabPanel;

	public Corps() {
		vp = new VerticalPanel();
		vp.setSize("100%", "100%");
		vp.setSpacing(10);
		
			vp.clear();
		DecoratedTabPanel decoratedTabPanel = new DecoratedTabPanel();
		
		vp.add(decoratedTabPanel);
		initWidget(vp);
		
		decoratedTabPanel.setAnimationEnabled(true);
		decoratedTabPanel.setSize("100%", "100%");
			decoratedTabPanel.getDeckPanel().setSize("100%", "100%");
		
		// sur la page d'acceuil : accueil et inscription
		Accueil accueil = new Accueil();
		accueil.setStyleName("centrer");
		decoratedTabPanel.add(accueil, "Accueil", false);
		accueil.setSize("100%", "100%");

		ACL_Inscription inscription = new ACL_Inscription();
		decoratedTabPanel.add(inscription, "Inscription", false);
		inscription.setSize("100%", "100%");

		vp.setSize("100%", "100%");
		decoratedTabPanel.selectTab(0);
	}

	@Override
	public void onSignOut(SignOutEvent signOutEvent) {
		vp.clear();
		DecoratedTabPanel decoratedTabPanel = new DecoratedTabPanel();
		vp.add(decoratedTabPanel);

		decoratedTabPanel.setAnimationEnabled(true);
		decoratedTabPanel.setSize("100%", "100%");
		decoratedTabPanel.getDeckPanel().setSize("100%", "100%");

		// une fois déconnecté : on retourne sur le menu de base
		Accueil accueil = new Accueil();
		accueil.setStyleName("centrer");
		decoratedTabPanel.add(accueil, "Accueil", false);
		accueil.setSize("100%", "100%");

		ACL_Inscription inscription = new ACL_Inscription();
		decoratedTabPanel.add(inscription, "Inscription", false);
		inscription.setSize("100%", "100%");

		vp.setSize("100%", "100%");
		decoratedTabPanel.selectTab(0);
		hr.removeHandler();
	}

	@Override
	public void onConnected(ConnectedEvent connectedEvent) {
		vp.clear();

		decoratedTabPanel = new TabLayoutPanel(30, Unit.PX);
		decoratedTabPanel.setAnimationDuration(200);
		// decoratedTabPanel.setStylePrimaryName("navigation");

		// une fois connecté : menu spécial
		// accueil, profil, recherche, réseau
		Accueil accueil = new Accueil();
		accueil.setLoggedIn();
		decoratedTabPanel.add(accueil, "Accueil", false);
		accueil.setSize("100%", "100%");

		Profil profil = new Profil();
		decoratedTabPanel.add(profil, "Mon Profil", false);
		profil.setSize("100%", "100%");
		// profil.onConnected(connectedEvent);

		RechercheDeContact recherche = new RechercheDeContact();
		decoratedTabPanel.add(recherche, "Recherche de contacts", false);
		recherche.setSize("100%", "100%");

		Reseau reseau = new Reseau();
		decoratedTabPanel.add(reseau, "Mon Réseau", false);
		reseau.setSize("100%", "100%");

		decoratedTabPanel.setSize("100%", "100%");
		// decoratedTabPanel.getDeckPanel().setSize("100%", "100%");
		decoratedTabPanel.selectTab(0);
		vp.add(decoratedTabPanel);
		vp.setSize("100%", "100%");
		hr = History.addValueChangeHandler(this);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String[] tab = event.getValue().split("/");
		if (tab.length > 1 && tab[0] != null && tab[0].equals("user")) {
			decoratedTabPanel.selectTab(2);
		}
		if (event.getValue().equals("news")
				|| event.getValue().equals("messagerie")) {
			decoratedTabPanel.selectTab(0);
		}

		if (event.getValue().equals("identite")
				|| event.getValue().equals("experience")
				|| event.getValue().equals("etudes")) {
			decoratedTabPanel.selectTab(1);
		}
		if (event.getValue().equals("recherche")
				|| event.getValue().equals("suggestion")
				|| event.getValue().equals("visualisation")) {
			decoratedTabPanel.selectTab(2);
		}
		if (event.getValue().equals("relations")
				|| event.getValue().equals("statistiques")) {
			decoratedTabPanel.selectTab(3);
		}
	}

}
